<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/marketingPlay' }"> <i class="el-icon-lx-cascades"></i> 营销玩法 </el-breadcrumb-item>
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 涨粉神器 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-button type="primary" class="handle-del mr10" @click="addDialog">新增涨粉神器</el-button>
            <div class="handle-box">
                <el-input v-model="query.keyword" placeholder="输入活动名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table :data="tableData" class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="name" label="活动名称"></el-table-column>
                <el-table-column label="活动状态">
                    <template slot-scope="scope">
                        <span
                            :style="
                                scope.row.time_status == 0 ? 'color: #e6a23c' : scope.row.time_status == 1 ? 'color: green' : 'color: #888'
                            "
                        >
                            {{ scope.row.time_status == 0 ? '未开始' : scope.row.time_status == 1 ? '进行中' : '已结束' }}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column prop="title" label="关联商品" width="300">
                    <template slot-scope="scope">
                        <div class="course-info-wrapper">
                            <div class="img-wrapper">
                                <img v-if="scope.row.info" :src="scope.row.info.cover" alt="" class="info-image" />
                            </div>
                            <div class="info-detail">
                                <div class="title">
                                    <span v-if="scope.row.info">{{ scope.row.info.title }}</span>
                                </div>
                                <div v-if="scope.row.info" class="price">{{ scope.row.info.price == 0 ? '免费' : '¥ ' + scope.row.info.price }}</div>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="keywords" label="关键词"></el-table-column>
                <el-table-column prop="plus_fen" label="活动共涨粉"></el-table-column>

                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-row>
                            <el-button @click="edit(scope.row)" v-if="scope.row.time_status == 0" type="success" size="mini"
                                >编辑</el-button
                            >
                            <el-button @click="end(scope.row.id)" v-if="scope.row.time_status !== 2" type="warning" size="mini"
                                >立即结束</el-button
                            >
                            <el-button @click="deleteRow(scope.row.id, scope.$index)" v-else type="danger" size="mini">删除</el-button>
                        </el-row>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.rows"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 新增涨粉神器弹出框 -->
        <el-dialog title="新增/编辑" :visible.sync="showDialog" width="30%">
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item label="关联商品">
                    <el-button @click="chooseCourse" type="primary" size="small">选择课程</el-button>
                    <span class="remindBox">只能添加一件商品参与涨粉神器活动</span>
                    <el-table
                        :data="selectRowData"
                        v-if="selectRowData.length !== 0"
                        class="table"
                        ref="multipleTable"
                        header-cell-class-name="table-header"
                    >
                        <el-table-column label="商品信息" width="250">
                            <template slot-scope="scope">
                                <div class="course-info-wrapper">
                                    <div class="img-wrapper">
                                        <img :src="scope.row.cover" alt="" class="info-image-mini" />
                                    </div>
                                    <div class="info-detail">
                                        <div class="title">
                                            <span>{{ scope.row.title }}</span>
                                        </div>
                                        <span>{{ scope.row.description }}</span>
                                    </div>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="类型">
                            <template slot-scope="scope">
                                <span>{{
                                    scope.row.type == 'image_text'
                                        ? '图文'
                                        : scope.row.type == 'audio'
                                        ? '音频'
                                        : scope.row.type == 'video'
                                        ? '视频'
                                        : '专栏'
                                }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="价格">
                            <template slot-scope="scope">
                                <span class="red">{{ '¥ ' + scope.row.price }}</span>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-form-item>
                <el-form-item label="活动名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="活动时间">
                    <el-date-picker
                        v-model="useTimeDate"
                        type="datetimerange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        value-format="yyyy-MM-dd HH:mm:ss"
                    >
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="海报文案">
                    <el-input type="textarea" :rows="2" v-model="form.content"> </el-input>
                </el-form-item>
                <el-form-item label="关键词">
                    <el-input v-model="form.keywords"></el-input>
                </el-form-item>
                <el-form-item label="拉新人数">
                    <el-input-number v-model="form.la_num" controls-position="right" :min="0"></el-input-number>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 选择产品类型 -->
        <el-dialog width="30%" title="选择产品类型" :visible.sync="productTypeVisible" append-to-body>
            <div class="product_type">
                <span v-for="item in productType" :key="item.id" @click="chooseType(item.value)">
                    {{ item.label }}
                </span>
            </div>
        </el-dialog>
        <!-- 打开内层弹框 -->
        <el-dialog width="36%" :title="`选择${productTypeTitle}`" @close="exit" :visible.sync="innerVisible" append-to-body>
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item>
                    <el-input v-model="query.keyword" :placeholder="`输入${productTypeTitle}名称`" class="handle-input mr10"></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="courseSearch">搜索</el-button>
                </el-form-item>
                <el-table
                    :data="chooseCourseTableData"
                    @row-click="chooseRow"
                    class="chooseCourseTable"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                >
                    <el-table-column :label="`${productTypeTitle}封面`">
                        <template slot-scope="scope">
                            <img :src="scope.row.cover" alt="" class="info-image" />
                        </template>
                    </el-table-column>
                    <el-table-column prop="title" :label="`${productTypeTitle}名称`"></el-table-column>
                    <el-table-column label="免费/付费">
                        <template slot-scope="scope">
                            <span style="color: #e65c5c">{{ scope.row.price == 0 ? '免费' : '¥ ' + scope.row.price }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="type" :label="`${productTypeTitle}类型`" v-if="productTypeTitle == '课程'">
                        <template slot-scope="scope">
                            {{ scope.row.type == 'video' ? '视频' : scope.row.type == 'audio' ? '音频' : '图文' }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="status" label="状态">
                        <template slot-scope="scope">
                            {{ scope.row.status == 0 ? '未上架' : '已上架' }}
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination">
                    <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.page"
                        :page-size="query.rows"
                        :total="courseListTotal"
                        @current-change="coursePageChange"
                    ></el-pagination>
                </div>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="exit">关 闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { increaseFansListData, videoListData, columnListData, examListData, addIncreaseFansData, delIncreaseFansData } from '../../api/index';
export default {
    // name: 'lecturer',
    data() {
        return {
            query: {
                page: 1,
                rows: 4,
                keyword: ''
            },
            form: {
                k_id: '',
                info_type: '',
                name: '',
                start_time: '',
                end_time: '',
                content: '',
                keywords: '',
                la_num: ''
            },
            productType: [
                {
                    value: 'knowledge',
                    label: '图文'
                },
                {
                    value: 'knowledge',
                    label: '音频'
                },
                {
                    value: 'knowledge',
                    label: '视频'
                },
                {
                    value: 'column',
                    label: '专栏'
                }
            ],
            nowDateTime: '',
            useTimeDate: [],
            tableData: [],
            chooseCourseTableData: [],
            selectRowData: [],
            productTypeTitle: '',
            showDialog: false,
            productTypeVisible: false,
            innerVisible: false,
            pageTotal: 0,
            courseListTotal: 0
        };
    },
    created() {
        this.getData();
        this.Date();
    },
    methods: {
        getData() {
            increaseFansListData(this.query).then((res) => {
                this.tableData = res.data.list;
                this.pageTotal = res.data.count;
            });
        },
        // 搜索
        handleSearch() {
            this.$set(this.query);
            this.getData();
        },
        // 分页
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        },
        // 课程分页
        coursePageChange(val) {
            this.$set(this.query, 'page', val);
            this.courseSearch();
        },
        //新增
        addDialog() {
            this.showDialog = true;
            this.selectRowData = [];
        },
        // 课程搜索
        courseSearch() {
            if (this.productTypeTitle == '课程') {
                videoListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            } else if (this.productTypeTitle == '专栏') {
                columnListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            } else {
                examListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            }
        },
        chooseCourse() {
            this.productTypeVisible = true;
        },
        chooseType(type) {
            this.innerVisible = true;
            this.query.page = 1;
            this.query.keyword = '';
            if (type == 'knowledge') {
                this.productTypeTitle = '课程';
                videoListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            } else {
                this.productTypeTitle = '专栏';
                columnListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            }
        },
        chooseRow(row) {
            console.log(row.info_type_str)
            this.selectRowData = [];
            this.selectRowData.push(row);
            this.form.k_id = row.id;
            if (row.type == 'column') {
                this.form.info_type = 'column';
            } else {
                this.form.info_type = 'knowledge';
            }
            this.innerVisible = false;
            this.productTypeVisible = false;
        },
        //确定
        save() {
            this.form.start_time = this.useTimeDate[0];
            this.form.end_time = this.useTimeDate[1];
            addIncreaseFansData(this.form).then((res) => {
                if (res.code == 1) {
                    this.showDialog = false;
                    this.$message.success('操作成功');
                    setTimeout(() => {
                        location.reload();
                    }, 200);
                }
            });
        },
        //编辑
        edit(row) {
            this.selectRowData = [];
            this.showDialog = true;
            this.form = row;
            this.selectRowData.push(row.info);
            this.useTimeDate = [row.start_time, row.end_time];
        },
        //获取当前年月日时分秒
        Date() {
            const nowDate = new Date();
            const date = {
                year: nowDate.getFullYear(),
                month: nowDate.getMonth() + 1,
                date: nowDate.getDate(),
                hours: nowDate.getHours(),
                minutes: nowDate.getMinutes(),
                seconds: nowDate.getSeconds()
            };
            const newmonth = date.month > 10 ? date.month : '0' + date.month;
            const newday = date.date > 10 ? date.date : '0' + date.date;
            const newminutes = date.minutes > 10 ? date.minutes : '0' + date.minutes;
            const newseconds = date.seconds > 10 ? date.seconds : '0' + date.seconds;
            this.nowDateTime = date.year + '-' + newmonth + '-' + newday + ' ' + date.hours + ':' + newminutes + ':' + newseconds;
        },
        // 结束
        end(id) {
            this.$confirm('确定立即结束吗？', '提示', {
                type: 'warning'
            }).then(() => {
                addIncreaseFansData({ id: id, end_time: this.nowDateTime }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('结束成功');
                        setTimeout(() => {
                            location.reload();
                        }, 200);
                    }
                });
            });
        },
        //删除
        deleteRow(id, index) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                delIncreaseFansData({ id: id }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('删除成功');
                        this.tableData.splice(index, 1);
                        setTimeout(() => {
                            location.reload();
                        }, 200);
                    }
                });
            });
        },
        //取消
        cancel() {
            this.showDialog = false;
            this.form = {};
            this.useTimeDate = '';
        },
        //关闭
        exit() {
            this.innerVisible = false;
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
    float: right;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}

.mr10 {
    margin-right: 10px;
}
.innerPagination {
    float: left;
}
.dialog-footer {
    display: block;
}
.valide_date {
    float: left;
}
.remindBox {
    margin-left: 15px;
    padding: 0 5px;
    display: inline-block;
    line-height: 22px;
    background-color: #daf1ff;
    border: solid 1px #a1daff;
    font-size: 12px;
    text-align: left;
    color: #578ce2;
}
/* 授权课程start */
.course-info-wrapper {
    display: flex;
    align-items: center;
    width: 100%;
    height: 64px;
    text-align: left;
    cursor: pointer;
}
.course-info-wrapper .img-wrapper {
    position: relative;
}
.info-image {
    height: 64px;
    width: 80px;
    border-radius: 4px;
}
.info-image-mini {
    height: 60px;
    width: 60px;
    border-radius: 50%;
}
.red {
    color: #ea644a;
}
.course-info-wrapper .info-detail {
    line-height: 1.5;
    height: 100%;
    margin-left: 8px;
}
.course-info-wrapper .info-detail .title {
    max-width: 100%;
    height: 44px;
    font-size: 14px;
    font-weight: 400;
    color: #333333;
    line-height: 22px;
    overflow: hidden;
    text-overflow: ellipsis;
    word-wrap: break-word;
    white-space: pre-wrap;
}
.course-info-wrapper .info-detail .price {
    font-size: 14px;
    font-weight: 400;
    color: #e65c5c;
}
.product_type {
    text-align: center;
}
.product_type span {
    cursor: pointer;
    display: inline-block;
    padding: 0 5px;
    margin: 0 20px;
    color: #fff;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 3px;
}
.chooseCourseTable {
    cursor: pointer;
}
/* 授权课程end */
</style>


